/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

class AbstractInventory;

/**
 * Provides a model for displaying Items in an AbstractInventory.
 */
class InventoryModel : public QAbstractItemModel
{
	Q_OBJECT

public:
	/**
	 * Constructs an inventory model.
	 *
	 * @param inventory The inventory who's contents are to be modeled.
	 */
	InventoryModel(AbstractInventory &inventory);

	/**
	 * Returns the index of the item in the model specified by the given row, column and parent index.
	 *
	 * @param row The item's row.
	 * @param column The item's column.
	 * @param parent The parent index.
	 */
	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;

	/**
	 * Returns the parent of the model item with the given index, or an invalid index if none exist.
	 *
	 * @param child The child to query.
	 */
	QModelIndex parent(const QModelIndex &child) const override;

	/**
	 * Returns the number of rows.
	 *
	 * @param parent Not used.
	 */
	int rowCount(const QModelIndex &parent = QModelIndex()) const override;

	/**
	 * Returns the number of columns.
	 *
	 * @param parent Not used.
	 */
	int columnCount(const QModelIndex &parent = QModelIndex()) const override;

	/**
	 * Returns the data stored under the given role for the item referred to by the index.
	 *
	 * @param index The index to query.
	 * @param role The role of the data to retrieve.
	 */
	QVariant data(const QModelIndex &index, int role) const override;

	/**
	 * Returns the data for the given role and section in the header.
	 * 
	 * @param section The header to retrieve.
	 * @param orientation The orientation of the data.
	 * @param role The role of the data.
	 */
	QVariant headerData(int section, Qt::Orientation orientation, int role = 0) const override;

	/**
	 * Sorts the model by column in the given order.
	 * 
	 * @param column The column to sort by.
	 * @param order The order to sort by.
	 */
	void sort(int column, Qt::SortOrder order) override;

	/**
	 * Gets the item (displayed by this model) at the specified index.
	 * 
	 * @param index The index of the item to retrieve.
	 */
	Item getItem(const QModelIndex index) const;

private slots:
	/**
	 * Refreshes the model (retrieves a new set of entries from the underlying inventory).
	 */
	void refresh();
	
private:
	AbstractInventory *inventory;
	QList<Item> items;

	std::function<bool (const Item &left, const Item &right)> sortComparisonFunc;
};

